#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class Computer{
public:
    Computer(const char* brand, int price)
    :_brand(new char[strlen(brand)+1]())
    ,_price(price)
    {
        strcpy(_brand,brand);
        _total_price += _price;
    }

    Computer & operator=(Computer & rhs){
        if(this != &rhs){
            delete [] _brand;
            _total_price -= _price;
            _brand = new char[strlen(rhs._brand)+1]();
            strcpy(_brand,rhs._brand);
            _price = rhs._price;
            _total_price += _price;
        }
        return *this;
    }
    static int get_total_price(){
        return _total_price;
    }

    ~Computer(){
        if(_brand != nullptr){
            delete [] _brand;
            _brand = nullptr;
            _total_price -= _price;
        }
    }

private:
    char* _brand;
    int _price;
    static int _total_price;
};
int Computer::_total_price = 0; //静态成员需要在类外初始化

void test(){
    Computer c1("huawei",1000);
    cout<<Computer::get_total_price()<<endl;
    Computer c2("xiaomi",1000);
    cout<<Computer::get_total_price()<<endl;
    Computer c3("rongyao",2000);
    cout<<Computer::get_total_price()<<endl;
    c3 = c2;
    cout<<Computer::get_total_price()<<endl;

}

int main()
{
    

    test ();
    return 0;
}

